package com.gexiaobao.pigeon.ui.pigeoncatcher

import android.content.Intent
import android.os.Bundle
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.util.ConstantsUtil
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivitySignBinding
import com.gexiaobao.pigeon.viewmodel.PigeonCatcherViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.finish
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2024/7/4 14:19
 * @Describe    : 签字
 */
class ActivitySign : BaseActivity<PigeonCatcherViewModel, ActivitySignBinding>() {

    private var mFilePath = ""

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "签字"

        mDatabind.includeBtn.tvRightBtn.text = "保存"
        mDatabind.includeBtn.tvLeftBtn.text = "清除"
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.includeBtn.tvRightBtn, mDatabind.includeBtn.tvLeftBtn) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    finish()
                }

                mDatabind.includeBtn.tvRightBtn -> {
                    signMethod()
                }

                mDatabind.includeBtn.tvLeftBtn -> {
                    cleanSign()
                }
            }
        }
    }

    private fun cleanSign() {
        mDatabind.gestureSignatureView.clear()
        mFilePath = ""
    }

    private fun signMethod() {
        if (!mDatabind.gestureSignatureView.touched) {
            RxToast.showToast("Please sign first")
            return
        }
        val file = File(ConstantsUtil.IMG_FOLDER_PATH)
        file.mkdirs()
        val fillPath: String = ConstantsUtil.IMG_FOLDER_PATH + "signImg" + System.currentTimeMillis() + ".jpg"
        mDatabind.gestureSignatureView.save(fillPath)
        mFilePath = fillPath
        val intent = Intent()
        intent.putExtra("filePath", mFilePath)
        finish(intent)
    }
}